'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	
	const db = uniCloud.database()
	const collection = db.collection('exam-result')
	
	// 判断条形码是否已存在
	let resultObj = await collection.where({
	  barcode: event.userInfo.barcode,
	  is_deleted: false
	}).count()
	console.log("resultObj：", resultObj)
	if (resultObj.total > 0) {
		return {
			code: '01',
			err_msg: '条形码已存在，请重新上传'
		}
	}
	
	event.userInfo.is_deleted = false;
	event.userInfo.create_time = new Date().getTime();
	event.userInfo.update_time = new Date().getTime();
	
	const res = await collection.add(event.userInfo);
	
	//返回数据给客户端
	return {
		code: '00',
		success_msg: '上传成功',
		data: res
	}
};
